<?php

namespace app\admin\model;

use think\Model;

class Collection extends Model
{
    // 表名
    protected $name = 'collection';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2')];
    }     


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    /**
     * 函数用途描述:API获取列表展示
     * @author: Feng_Ke
     * @date: 2018/12/6 14:41
     * @param array $where
     * @param string $field
     * @return false|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($where=[],$order='',$limlt='',$field=''){
        $list = $this
            ->where($where)
            ->order($order)
            ->limit($limlt)
            ->field($field)
            ->select();
        return $list;
    }

}
